package swimv2.session;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import swimv2.entity.*;

import org.jboss.ejb3.annotation.RemoteBinding;

/**
 * Session Bean implementation class HypertextControllerBean
 */
@Stateless
@RemoteBinding( jndiBinding = "HypertextController" )
public class HypertextControllerBean implements IHypertextControllerRemote {
	@PersistenceContext(unitName = "swimv2")
	protected EntityManager manager;
    /**
     * Default constructor. 
     */
    public HypertextControllerBean() {
        // TODO Auto-generated constructor stub
    }
	@Override
	public boolean viewCheck(String vista, Integer id_User) {
		View v = (View) manager.createQuery("SELECT v FROM View v WHERE v.user.id = :id AND v.name= :name")
				.setParameter("id", id_User)
				.setParameter("name", vista)
				.getSingleResult();
				
		return (v!=null);
	}
	@Override
	public boolean isValidView(String vista) {
		
		View v = (View) manager.createQuery("SELECT v FROM View v WHERE v.name = :name")
				.setParameter("name", vista)
				.getSingleResult();
		
		return (v!=null);
	}
	@Override
	public List<View> getDefaultView(Integer id_User) {
		
		List<View> v = (List<View>) manager.createQuery("SELECT v FROM View v JOIN v.user u WHERE u.id = :id")
		.setParameter("id", id_User)
		.getResultList();
		
		return v;
	}

}
